﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;

namespace InternetShop.Code
{
    public class Order
    {
        #region Properties

        public string CustomerName { get; set; }

        public string MarchandiserName { get; set; }

        public bool isCompleted { get; set; }

        public DateTime? CreateDate { get; set; }

        public long ID { get; set; }

        private Dictionary<Product, int> _goods = new Dictionary<Product, int>();

        public Dictionary<Product, int> Goods 
        {
            get
            {
                return _goods;
            }
        }

        #endregion

        #region Constructors

        public Order() { }

        public Order(Basket bkt)
        {
            
        }

        #endregion

        #region Methods

       /* public decimal getCost()
        {
            decimal price = 0;
            foreach (var i in _goods)
            {
                try{
                    if (i.Price != null && i.Quantity != null)
                        price += i.Price * i.Quantity;

                }
                catch(Exception e)
                {
                }
            }
            return price;
        }*/

        public static Order GetByID(long id)
        {
            Order result = new Order();
            SqlParameter[] dbParams = {
                                          new SqlParameter("@ID", id)
                                      };
            DataHelper.ExecFillingProc("OrderGetByID", dbParams,
                delegate(IDataReader r)
                {
                    while (r.Read())
                    {
                        result.ID = id;
                        result.MarchandiserName = (string)r["MerchandiserName"];
                        result.CustomerName = (string)r["CustomerName"];
                        result.isCompleted = (bool)r["IsCompleted"];
                        result.CreateDate = (DateTime)r["CreateDate"];
                    }
                });
            DataHelper.ExecFillingProc("OrderGetItems", dbParams,
                delegate(IDataReader r)
                {
                    while (r.Read())
                    {
                        Product prdct;
                        var productID = (long)r["ProductID"];
                        var quantity = (long)r["Quantity"];
                        prdct = Product.LoadById(productID);
                        result.Goods.Add(prdct, (int)quantity);
                    }
                }
             );
            return result;
        }

        public static void test()
        {
            
        }
  
        #endregion
    }
}